package com.yizisu.playerlibrary.service.download

import android.app.Notification
import android.net.Uri
import android.os.Environment
import androidx.annotation.DrawableRes
import com.google.android.exoplayer2.database.ExoDatabaseProvider
import com.google.android.exoplayer2.ext.workmanager.WorkManagerScheduler
import com.google.android.exoplayer2.offline.Download
import com.google.android.exoplayer2.offline.DownloadManager
import com.google.android.exoplayer2.offline.DownloadRequest
import com.google.android.exoplayer2.offline.DownloadService
import com.google.android.exoplayer2.scheduler.Scheduler
import com.google.android.exoplayer2.ui.DownloadNotificationHelper
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor
import com.google.android.exoplayer2.upstream.cache.SimpleCache
import com.google.android.exoplayer2.util.Log
import com.yizisu.playerlibrary.PlayerLibInit
import com.yizisu.playerlibrary.R
import com.yizisu.playerlibrary.helper.PlayerModel
import java.io.File
import java.io.IOException

class PlayerDownloadService : DownloadService {

    constructor() : this(++PlayerDownload.notificationId)

    constructor(foregroundNotificationId: Int) : this(
        foregroundNotificationId,
        DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL
    )

    constructor(foregroundNotificationId: Int, foregroundNotificationUpdateInterval: Long) : this(
        foregroundNotificationId,
        foregroundNotificationUpdateInterval,
        PlayerDownload.channelId,
        R.string.app_name,
        R.string.app_name,
    )

    constructor(
        foregroundNotificationId: Int,
        foregroundNotificationUpdateInterval: Long,
        channelId: String?,
        channelNameResourceId: Int,
        channelDescriptionResourceId: Int
    ) : super(
        foregroundNotificationId,
        foregroundNotificationUpdateInterval,
        channelId,
        channelNameResourceId,
        channelDescriptionResourceId
    )

    override fun getDownloadManager(): DownloadManager {
        return PlayerDownload.downloadManager
    }

    override fun getScheduler(): Scheduler? {
        return WorkManagerScheduler(this, "exo_player_cache")
    }

    override fun getForegroundNotification(downloads: MutableList<Download>): Notification {
        PlayerDownload.notificationListeners.toList().forEach {
            it.updateNotification(downloads)
        }
        val updateNotificationUi = PlayerDownload.updateNotificationUi?.invoke(downloads)
        val message = updateNotificationUi?.message
        val contentIntent = updateNotificationUi?.contentIntent
        val smallIcon = updateNotificationUi?.smallIcon ?: android.R.drawable.stat_sys_download
        return DownloadNotificationHelper(this, PlayerDownload.channelId)
            .buildProgressNotification(
                this,
                smallIcon,
                contentIntent,
                message,
                downloads
            )
    }
}